<div xmlns:nxu="http://nuxeo.org/nxweb/util"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:nxl="http://nuxeo.org/nxforms/layout"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:c="http://java.sun.com/jstl/core"
    xmlns:a4j="http://richfaces.org/a4j">

  <h:outputScript target="#{pageJsTarget}">
    function confirmPurgePermissions() {
      return confirm("#{messages['label.permissions.purge.confirmPurgePermissions']}");
    }
  </h:outputScript>

  <a4j:region>

    <a4j:outputPanel id="permissions_purge_panel" layout="block">

      <nxu:set var="purgeStatus" value="#{adminPermissionsActions.purgeStatus}">
        <nxu:set var="purgeScheduled" value="#{not empty purgeStatus and purgeStatus.isScheduled()}">
          <nxu:set var="purgeRunning" value="#{not empty purgeStatus and purgeStatus.isRunning()}">
            <nxu:set var="purgeComplete" value="#{not empty purgeStatus and purgeStatus.isComplete()}">

              <a4j:poll interval="5000"
                  render="permissions_purge_panel"
                  enabled="#{purgeScheduled or purgeRunning}">
                <a4j:attachQueue ignoreDupResponses="true" />
              </a4j:poll>

              <c:if test="#{not purgeScheduled and not purgeRunning and not purgeComplete}">
                <nxu:set var="contentViewName" value="PERMISSIONS_PURGE">
                  <nxu:set var="contentView"
                      value="#{contentViewActions.getContentViewWithProvider(contentViewName, contentViewDocument, finalSortInfos, finalDefaultPageSize, -1, -1)}"
                      cache="true">

                    <c:if test="#{!contentView.searchDocumentModel.dirty}">
                      <h3>#{messages['label.permissions.purge.step1']}</h3>
                      <h:form execute="@this">
                        <nxl:layout name="#{contentView.searchLayout.name}" mode="edit"
                            value="#{contentView.searchDocumentModel}"/>

                        <a4j:commandButton value="#{messages['command.search']}"
                            action="#{contentViewActions.refreshAndRewind(contentViewName)}"
                            render="permissions_purge_panel"
                            styleClass="button primary"/>
                      </h:form>
                    </c:if>

                    <c:if test="#{contentView.searchDocumentModel.dirty}">
                      <h3>#{messages['label.permissions.purge.step2']}</h3>
                      <ui:decorate template="/incl/content_view.xhtml">
                        <ui:define name="content_view_title"/>
                        <ui:define name="content_view_syndication_buttons"/>
                      </ui:decorate>
                      <h:form execute="@this">
                        <a4j:commandButton value="#{messages['command.cancel']}"
                            action="#{adminPermissionsActions.cancelPurge()}"
                            render="permissions_purge_panel"
                            styleClass="button"/>
                        <a4j:commandButton value="#{messages['command.permissions.purge']}"
                            action="#{adminPermissionsActions.doPurge()}"
                            render="permissions_purge_panel"
                            disabled="#{empty contentView.currentPageProvider.currentPage}"
                            styleClass="button primary"
                            onclick="if (!confirmPurgePermissions()) { return false; }"/>
                        <span class="detail">#{messages['label.permissions.purge.confirm']}</span>
                      </h:form>
                    </c:if>

                  </nxu:set>
                </nxu:set>
              </c:if>

              <c:if test="#{purgeScheduled}">
                <h3>#{messages['label.permissions.purge.step2']}</h3>
                <h:outputText styleClass="processMessage scheduled" value="#{messages['label.permissions.purgeScheduled']}"/>
              </c:if>

              <c:if test="#{purgeRunning}">
                <h3>#{messages['label.permissions.purge.step2']}</h3>
                <h:outputText styleClass="processMessage running" value="#{messages['label.permissions.purgeRunning']}"/>
              </c:if>

              <c:if test="#{purgeComplete}">
                <h3>#{messages['label.permissions.purge.step2']}</h3>
                <h:outputText styleClass="processMessage completeSuccess" value="#{messages['label.permissions.purgeComplete']}"/>

                <h:form execute="@this">
                  <a4j:commandButton value="#{messages['command.permissions.purge.startNew']}"
                      action="#{adminPermissionsActions.cancelPurge()}"
                      render="permissions_purge_panel"
                      styleClass="button primary"/>
                </h:form>
              </c:if>
            </nxu:set>
          </nxu:set>
        </nxu:set>
      </nxu:set>
    </a4j:outputPanel>

  </a4j:region>

</div>
